package com.ckzp.weixin.wxpay;

import com.ckzp.core.ConfigUtil;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.util.DateUtil;
import com.ckzp.core.util.StringUtil;
import com.ckzp.weixin.wxpay.data.RefundData;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2019/11/21.
 */
public class MyPayUtil {


    public static MyPayUtil n() {
        return new MyPayUtil();
    }

    /**
     * 取得订购参数
     *
     * @param pd
     * @return
     */
    public static JsApi getJsApi(PayData pd) {
        return getJsApi(0, pd.getOpen_id(), pd.getTrade_no(), pd.getPre_bodys(), pd.getPre_proid(), pd.getPre_fees(), "JSAPI");
    }

    /**
     * 取得订购参数
     *
     * @param pd
     * @return
     */
    public static JsApi getJsApi(int configID, PayData pd) {
        return getJsApi(configID, pd.getOpen_id(), pd.getTrade_no(), pd.getPre_bodys(), pd.getPre_proid(), pd.getPre_fees(), "JSAPI");
    }

    /**
     * 取得订购参数
     *
     * @param openid
     * @param trade_no
     * @param bodys
     * @param product_id
     * @param fees
     * @return
     */
    public static JsApi getJsApi(String openid, String trade_no, String bodys, String product_id, int fees) {
        return getJsApi(0, openid, trade_no, bodys, product_id, fees, "JSAPI");
    }

    /**
     * 取得订购参数
     *
     * @param openid
     * @param trade_no
     * @param bodys
     * @param product_id
     * @param fees
     * @return
     */
    public static JsApi getJsApi(int configID, String openid, String trade_no, String bodys, String product_id, int fees, String types) {
        JsApi api = JsApi.n().setCode(9);
        try {
            Map<String, String> result = unifiedOrder(configID, openid, trade_no, bodys, product_id, fees, types);
            String result_code = StringUtil.getString(result, "result_code");
            if ("SUCCESS".equals(result_code)) {
                api.setCode(0).setConfigID(configID)
                        .setAppId(StringUtil.getString(result, "appid"))
                        .setTimeStamp(String.valueOf(DateUtil.getTime()))
                        .setSignType("MD5")
                        .setPackages("prepay_id=" + StringUtil.getString(result, "prepay_id"))
                        .setNonceStr(StringUtil.getString(result, "nonce_str"))
                        .setCode_url(StringUtil.getString(result, "code_url"))
                        .setPaySign();
            } else {
                api.setCode(9).setErrmsg("Error:" + result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            api.setCode(9).setErrmsg("Error:" + e.getMessage());

        }
        return api;
    }

    /**
     * @param openid
     * @param trade_no
     * @param bodys
     * @param product_id
     * @param fees
     * @param types
     * @return
     */
    public static Map<String, String> unifiedOrder(int configid, String openid, String trade_no, String bodys, String product_id, int fees, String types) {
        try {
            MyConfig configs = new MyConfig(configid);
            WXPay wxpay = new WXPay(configs);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", bodys);
            data.put("out_trade_no", trade_no);
            data.put("device_info", "001");//1561017141
            //data.put("sign_type", "MD5");
            data.put("fee_type", "CNY");
            data.put("total_fee", String.valueOf(fees));
            data.put("spbill_create_ip", "123.12.12.123");
            data.put("notify_url", ConfigUtil.getProperty("WX_PAYBACKURL"));
            data.put("trade_type", types);  // 此处指定为扫码支付
            data.put("openid", openid);
            data.put("product_id", product_id);
//            System.out.println("appid----------------------------" + configs.getAppID());
//            System.out.println("key  ----------------------------" + configs.getKey());
//            System.out.println("mchid----------------------------" + configs.getMchID());
//            System.out.println("configs----------------------------" + data.toString());
            return wxpay.unifiedOrder(data);
        } catch (Exception e) {
            e.printStackTrace();
            Map m = new HashMap();
            m.put("errinfo", e.getMessage());
            return m;
        }
    }

    public static Map<String, String> unifiedOrder(PayData pd) {
        try {
            MyConfig configs = new MyConfig();
            WXPay wxpay = new WXPay(configs);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", pd.getPre_bodys());
            data.put("out_trade_no", pd.getTrade_no());
            data.put("device_info", "001");//1561017141
            //data.put("sign_type", "MD5");
            data.put("fee_type", "CNY");
            data.put("total_fee", String.valueOf(pd.getPre_fees()));
            data.put("spbill_create_ip", "123.12.12.123");
            data.put("notify_url", ConfigUtil.getProperty("WX_PAYBACKURL"));
            data.put("trade_type", pd.getPay_types());  // 此处指定为扫码支付
            data.put("openid", pd.getOpen_id());
            data.put("product_id", pd.getPre_proid());
            return wxpay.unifiedOrder(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }

    /**
     * 微信退款
     *
     * @param pd
     * @return
     */
    public static Map<String, String> refund(RefundData pd) {
        try {
            MyConfig configs = new MyConfig();
            WXPay wxpay = new WXPay(configs);
            Map data = new HashMap<String, Object>();
            //data.put("body", pd.getPre_bodys());
            data.put("out_trade_no", pd.getOut_trade_no());
            data.put("out_refund_no", pd.getOut_refund_no());
            // data.put("notify_url", pd.getNotify_url());
            data.put("refund_desc", pd.getReason());//退款说明
            //data.put("sign_type", "MD5");
            data.put("refund_fee_type", "CNY");
            data.put("refund_fee", pd.getAmount().get("refund"));
            data.put("total_fee", pd.getAmount().get("total"));
            data.put("refund_account", "REFUND_SOURCE_RECHARGE_FUNDS");
            System.out.println(JsonUtil.toJsonString(data));
            return wxpay.refund(data);
        } catch (Exception e) {
            e.printStackTrace();
            HashMap map = new HashMap();
            map.put("msg", e.getMessage());
            return map;
        }
    }
}
